{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Pandas的索引操作",
   "id": "74865818e796e208"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## Series索引",
   "id": "fed5eb13e2dfd2e8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:42:07.102987Z",
     "start_time": "2025-01-13T12:42:06.662398Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "dict_data = {'A': 1,\n",
    "             'B': pd.Timestamp('20190926'),\n",
    "             'C': pd.Series(1, index=list(range(4)),dtype='float32'),\n",
    "             'D': np.array([1,2,3,4],dtype='int32'),\n",
    "             'E': [\"Python\",\"Java\",\"C++\",\"C\"],\n",
    "             'F': 'wangdao' }\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2.index)\n",
    "\n",
    "#索引对象的值不可变（上面代码增加）\n",
    "# df_obj2.index[0] = 2"
   ],
   "id": "dbdc2fd1565d2e1c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index([0, 1, 2, 3], dtype='int64')\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "Index does not support mutable operations",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[3], line 14\u001B[0m\n\u001B[0;32m     11\u001B[0m \u001B[38;5;28mprint\u001B[39m(df_obj2\u001B[38;5;241m.\u001B[39mindex)\n\u001B[0;32m     13\u001B[0m \u001B[38;5;66;03m#索引对象的值不可变（上面代码增加）\u001B[39;00m\n\u001B[1;32m---> 14\u001B[0m \u001B[43mdf_obj2\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mindex\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m]\u001B[49m \u001B[38;5;241m=\u001B[39m \u001B[38;5;241m2\u001B[39m\n",
      "File \u001B[1;32mC:\\Program Files\\Python312\\Lib\\site-packages\\pandas\\core\\indexes\\base.py:5371\u001B[0m, in \u001B[0;36mIndex.__setitem__\u001B[1;34m(self, key, value)\u001B[0m\n\u001B[0;32m   5369\u001B[0m \u001B[38;5;129m@final\u001B[39m\n\u001B[0;32m   5370\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m__setitem__\u001B[39m(\u001B[38;5;28mself\u001B[39m, key, value) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[1;32m-> 5371\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mTypeError\u001B[39;00m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mIndex does not support mutable operations\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n",
      "\u001B[1;31mTypeError\u001B[0m: Index does not support mutable operations"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:43:10.305078Z",
     "start_time": "2025-01-13T12:43:10.296730Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 常见的Index种类\n",
    "# •Index，索引  可以是各种类型\n",
    "# •Int64Index，整数索引\n",
    "# •MultiIndex，层级索引，难度较大\n",
    "# •DatetimeIndex，时间戳类型\n",
    "\n",
    "ser_obj = pd.Series(range(5), index = list(\"abcde\"))\n",
    "print(ser_obj)\n",
    "ser_obj.index"
   ],
   "id": "9ae74a3447365a9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c', 'd', 'e'], dtype='object')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:46:22.051185Z",
     "start_time": "2025-01-13T12:46:22.047558Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 行索引，不仅可以用索引名，还可以用索引位置或来取\n",
    "# print(ser_obj['b']) #索引名\n",
    "# print(ser_obj[2]) #位置索引\n",
    "print(ser_obj.loc['b']) #索引名,用.loc[]比较规范\n",
    "print(ser_obj.iloc[2]) #位置索引,用.iloc[]比较规范"
   ],
   "id": "5a2bc80b6921d10e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:47:13.482266Z",
     "start_time": "2025-01-13T12:47:13.475299Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 切片索引\n",
    "print(ser_obj.iloc[1:3])  #索引位置取数据，左闭右开\n",
    "print(ser_obj.loc['b':'d'])  #记住索引名  左闭右闭"
   ],
   "id": "85a4128f6fa0484c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:47:56.152747Z",
     "start_time": "2025-01-13T12:47:56.146808Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 不连续索引\n",
    "print(ser_obj.iloc[[0, 2, 4]])\n",
    "print(ser_obj.loc[['a', 'e']])"
   ],
   "id": "34873deae802660f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "c    2\n",
      "e    4\n",
      "dtype: int64\n",
      "a    0\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:50:15.192461Z",
     "start_time": "2025-01-13T12:50:15.187611Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 布尔索引\n",
    "ser_bool = ser_obj > 2\n",
    "print(ser_obj)\n",
    "print(ser_bool)\n",
    "\n",
    "print('-'*50)\n",
    "print(ser_obj[ser_bool]) #取出布尔索引为True的元素\n",
    "\n",
    "print(ser_obj[ser_obj > 2]) #取出大于2的元素"
   ],
   "id": "4079167f18be8bae",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "a    False\n",
      "b    False\n",
      "c    False\n",
      "d     True\n",
      "e     True\n",
      "dtype: bool\n",
      "--------------------------------------------------\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:49:12.243164Z",
     "start_time": "2025-01-13T12:49:12.237463Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "abcdc0920698d738",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##  DataFrame索引",
   "id": "5b6d5a13dfd6b8d6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:52:13.315425Z",
     "start_time": "2025-01-13T12:52:13.309894Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "df_obj = pd.DataFrame(np.random.randn(5,4),\n",
    "                      columns = ['a', 'b', 'c', 'd']) #设置列索引，行索引默认从0开始\n",
    "print(df_obj.head())"
   ],
   "id": "123f55e4f9773039",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "0  0.385904 -0.571808  0.182336  0.707422\n",
      "1 -1.577584  0.906338 -0.883227 -0.671945\n",
      "2  0.659146  0.029975  0.117807 -0.052334\n",
      "3  0.411770 -0.270201  0.661360 -0.848359\n",
      "4  0.865809  0.354089 -0.466087  0.387902\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:53:57.215954Z",
     "start_time": "2025-01-13T12:53:57.208183Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 列索引\n",
    "print(df_obj['a']) # 返回Series类型\n",
    "print('-'*50)\n",
    "print(df_obj[['a']]) # 返回DataFrame类型\n",
    "print('-'*50)\n",
    "print(type(df_obj[['a']])) # 返回DataFrame类型"
   ],
   "id": "71d1038e0257fd73",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.385904\n",
      "1   -1.577584\n",
      "2    0.659146\n",
      "3    0.411770\n",
      "4    0.865809\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "          a\n",
      "0  0.385904\n",
      "1 -1.577584\n",
      "2  0.659146\n",
      "3  0.411770\n",
      "4  0.865809\n",
      "--------------------------------------------------\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "1. loc 标签索引(通过索引标签值获取数据)",
   "id": "ea37eadda80c1dea"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:56:24.898017Z",
     "start_time": "2025-01-13T12:56:24.893165Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 标签索引 loc，建议使用loc，效率更高\n",
    "# Series\n",
    "print(ser_obj)\n",
    "print(ser_obj['b':'d'])\n",
    "print(ser_obj.loc['b':'d']) #前闭后闭\n",
    "print('-'*50)"
   ],
   "id": "3fe544abca9206f8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T12:57:20.650500Z",
     "start_time": "2025-01-13T12:57:20.643907Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame，行索引与列索引有相同的标签时\n",
    "df_obj = pd.DataFrame(np.random.randn(5,4),\n",
    "                      columns = list('abcd'),\n",
    "                      index=list('abcde'))\n",
    "print(df_obj)\n",
    "print('-'*50)\n",
    "print(df_obj['a'])  #不加loc,拿的是列\n",
    "print('-'*50)\n",
    "print(df_obj.loc['a'])  #加loc,拿的是行\n",
    "print('-'*50)"
   ],
   "id": "e01456c70b9cc741",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a  0.535294  0.698833  1.165393 -1.515736\n",
      "b  0.301467 -0.397980  1.193362  0.940298\n",
      "c -0.234911  0.343186 -0.770380 -0.051644\n",
      "d  1.259957 -0.140497  0.112821 -0.337057\n",
      "e  0.067478  1.002044 -0.195009  1.023113\n",
      "--------------------------------------------------\n",
      "a    0.535294\n",
      "b    0.301467\n",
      "c   -0.234911\n",
      "d    1.259957\n",
      "e    0.067478\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "a    0.535294\n",
      "b    0.698833\n",
      "c    1.165393\n",
      "d   -1.515736\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:00:24.531293Z",
     "start_time": "2025-01-13T13:00:24.522170Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 第一个参数索引行，第二个参数是列,loc或者iloc效率高于直接用取下标的方式，前闭后闭\n",
    "print(df_obj.loc['a':'c', 'b':'d']) #连续索引\n",
    "print(df_obj.loc[['a','c'], ['b','d']]) #不连续索引\n",
    "print(df_obj.loc[['c'],['b']]) #取一个值,返回的是DataFrame类型\n",
    "print(df_obj.loc['c','b'])  #取一个值"
   ],
   "id": "2fda5f184bc605a4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          b         c         d\n",
      "a  0.698833  1.165393 -1.515736\n",
      "b -0.397980  1.193362  0.940298\n",
      "c  0.343186 -0.770380 -0.051644\n",
      "          b         d\n",
      "a  0.698833 -1.515736\n",
      "c  0.343186 -0.051644\n",
      "          b\n",
      "c  0.343186\n",
      "0.34318633620549055\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## iloc 位置索引(推荐使用)",
   "id": "c8eccdeaaa364e67"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:02:53.053270Z",
     "start_time": "2025-01-13T13:02:53.048545Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(ser_obj)\n",
    "print('-'*50)\n",
    "# Series\n",
    "print(ser_obj[1:3])\n",
    "print('-'*50)\n",
    "print(ser_obj.iloc[1:3]) # 前闭后开[)，效率高\n"
   ],
   "id": "aec5bba0fa72cf72",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:04:05.031303Z",
     "start_time": "2025-01-13T13:04:05.023813Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame，iloc是前闭后开[)\n",
    "print(df_obj)\n",
    "print('-'*50)\n",
    "print(df_obj.iloc[0:2, 0:2]) \n",
    "print('-'*50)\n",
    "print(df_obj.iloc[[0,2], [0,2]]) # 不连续索引\n",
    "print('-'*50)\n",
    "print(df_obj.iloc[0,0]) # 取一个值"
   ],
   "id": "1b72f1184ced24",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a  0.535294  0.698833  1.165393 -1.515736\n",
      "b  0.301467 -0.397980  1.193362  0.940298\n",
      "c -0.234911  0.343186 -0.770380 -0.051644\n",
      "d  1.259957 -0.140497  0.112821 -0.337057\n",
      "e  0.067478  1.002044 -0.195009  1.023113\n",
      "--------------------------------------------------\n",
      "          a         b\n",
      "a  0.535294  0.698833\n",
      "b  0.301467 -0.397980\n",
      "--------------------------------------------------\n",
      "          a         c\n",
      "a  0.535294  1.165393\n",
      "c -0.234911 -0.770380\n",
      "--------------------------------------------------\n",
      "0.5352936561060889\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:10:44.331209Z",
     "start_time": "2025-01-13T13:10:44.323077Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#没有设置行和列索引的DataFrame，iloc和loc的区别\n",
    "df_obj2 = pd.DataFrame(np.random.randn(5,4))\n",
    "print(df_obj2)\n",
    "print('-'*50)\n",
    "print(df_obj2.iloc[0:2]) #左闭右开 2行\n",
    "print('-'*50)\n",
    "print(df_obj2.loc[0:2]) #左闭右闭 3行\n",
    "print('-'*50)\n",
    "print(df_obj2.iloc[:,0:2]) #列\n",
    "print(df_obj2.loc[:,0:2]) #列\n"
   ],
   "id": "b95937a94a43a11f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0 -1.074585  1.507034  3.652345 -0.196535\n",
      "1 -0.475337 -0.006069  0.886501  1.008946\n",
      "2 -0.148482  0.738529  2.536557 -0.985353\n",
      "3 -0.356420 -0.572048  2.983152  1.524834\n",
      "4 -0.764710  1.358579  0.444842 -0.305372\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0 -1.074585  1.507034  3.652345 -0.196535\n",
      "1 -0.475337 -0.006069  0.886501  1.008946\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0 -1.074585  1.507034  3.652345 -0.196535\n",
      "1 -0.475337 -0.006069  0.886501  1.008946\n",
      "2 -0.148482  0.738529  2.536557 -0.985353\n",
      "--------------------------------------------------\n",
      "          0         1\n",
      "0 -1.074585  1.507034\n",
      "1 -0.475337 -0.006069\n",
      "2 -0.148482  0.738529\n",
      "3 -0.356420 -0.572048\n",
      "4 -0.764710  1.358579\n",
      "          0         1         2\n",
      "0 -1.074585  1.507034  3.652345\n",
      "1 -0.475337 -0.006069  0.886501\n",
      "2 -0.148482  0.738529  2.536557\n",
      "3 -0.356420 -0.572048  2.983152\n",
      "4 -0.764710  1.358579  0.444842\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 对齐运算",
   "id": "87af899ec871dbbd"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:12:52.626529Z",
     "start_time": "2025-01-13T13:12:52.618182Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "s1 = pd.Series(range(10, 20), index = range(10))\n",
    "s2 = pd.Series(range(20, 25), index = range(5))\n",
    "# Series 对齐运算\n",
    "print('s1+s2: ')\n",
    "s3=s1+s2\n",
    "print(s3)  #缺失数据默认填充为NaN  np.nan"
   ],
   "id": "c79018df0e3bc31e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s1+s2: \n",
      "0    30.0\n",
      "1    32.0\n",
      "2    34.0\n",
      "3    36.0\n",
      "4    38.0\n",
      "5     NaN\n",
      "6     NaN\n",
      "7     NaN\n",
      "8     NaN\n",
      "9     NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:15:12.393086Z",
     "start_time": "2025-01-13T13:15:12.389545Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#两个长度不同的一维ndarray相加\n",
    "a1 = np.array([1,2,3,4,5])\n",
    "a2 = np.array([1]) # 长度为1\n",
    "print(a2.shape)\n",
    "print(a1+a2) #广播机制\n"
   ],
   "id": "9bc58445f40cb08e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1,)\n",
      "[2 3 4 5 6]\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:17:02.394504Z",
     "start_time": "2025-01-13T13:17:02.388620Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#series的对齐运算\n",
    "print(s2)\n",
    "print(s1)\n",
    "print(np.isnan(s3[6]))\n",
    "print('-'*50)\n",
    "print(s2.add(s1, fill_value = 0))  #2+1,未对齐的数据将和填充值做运算\n",
    "print(s2.sub(s1, fill_value = 0))  #2-1,未对齐的数据将和填充值做运算"
   ],
   "id": "46e739c47a7f2ae",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    20\n",
      "1    21\n",
      "2    22\n",
      "3    23\n",
      "4    24\n",
      "dtype: int64\n",
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "3    13\n",
      "4    14\n",
      "5    15\n",
      "6    16\n",
      "7    17\n",
      "8    18\n",
      "9    19\n",
      "dtype: int64\n",
      "True\n",
      "--------------------------------------------------\n",
      "0    30.0\n",
      "1    32.0\n",
      "2    34.0\n",
      "3    36.0\n",
      "4    38.0\n",
      "5    15.0\n",
      "6    16.0\n",
      "7    17.0\n",
      "8    18.0\n",
      "9    19.0\n",
      "dtype: float64\n",
      "0    10.0\n",
      "1    10.0\n",
      "2    10.0\n",
      "3    10.0\n",
      "4    10.0\n",
      "5   -15.0\n",
      "6   -16.0\n",
      "7   -17.0\n",
      "8   -18.0\n",
      "9   -19.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:22:04.575823Z",
     "start_time": "2025-01-13T13:22:04.560759Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#df的对齐运算\n",
    "import numpy as np\n",
    "df1 = pd.DataFrame(np.ones((2,2)), columns = ['a', 'b']) #ones((2,2))生成2行2列全1的DataFrame\n",
    "df2 = pd.DataFrame(np.ones((3,3)), columns = ['a', 'b', 'c']) #ones((3,3))生成3行3列全1的DataFrame\n",
    "print(df1)\n",
    "print(df2)\n",
    "print('-'*50)\n",
    "print(df2.dtypes)\n",
    "print(df1-df2)\n",
    "print(df2.sub(df1, fill_value = 2)) #未对齐的数据将和填充值做运算\n",
    "\n",
    "#总结：对于series和dataframe的对齐运算\n",
    "# 没对齐的元素，默认填充NaN，对齐运算时，fill_value参数可以指定填充值。"
   ],
   "id": "374b0a29139d4231",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     a    b\n",
      "0  1.0  1.0\n",
      "1  1.0  1.0\n",
      "     a    b    c\n",
      "0  1.0  1.0  1.0\n",
      "1  1.0  1.0  1.0\n",
      "2  1.0  1.0  1.0\n",
      "--------------------------------------------------\n",
      "a    float64\n",
      "b    float64\n",
      "c    float64\n",
      "dtype: object\n",
      "     a    b   c\n",
      "0  0.0  0.0 NaN\n",
      "1  0.0  0.0 NaN\n",
      "2  NaN  NaN NaN\n",
      "     a    b    c\n",
      "0  0.0  0.0 -1.0\n",
      "1  0.0  0.0 -1.0\n",
      "2 -1.0 -1.0 -1.0\n"
     ]
    }
   ],
   "execution_count": 33
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
